﻿using System;
using System.ComponentModel.DataAnnotations;

namespace Mini.Common
{
    public static class EnumHelper
    {
        public static T ConvertValueToEnum<T>(this string value) where T : struct
        {
            T t;
            Enum.TryParse(value, true, out t);
            return t;
        }

        public static string ConvertEnumKeyToString<T>(this T value)
        {
            return Enum.GetName(typeof(T), value);
        }

        public static string GetEnumDisplayName<T>(this T value)
        {
            var type = typeof(T);
            var memberInfo = type.GetMember(value.ToString());
            var attributes = memberInfo[0].GetCustomAttributes(typeof(DisplayAttribute), false);
            var description = ((DisplayAttribute)attributes[0]).Name;
            return description;
        }

        public static string ConvertEnumKeyToLower<T>(this T value)
        {
            var name = Enum.GetName(typeof(T), value);
            return !string.IsNullOrEmpty(name) ? name.ToLower() : string.Empty;
        }

        public static Array GetValues<T>(this T t)
        {
            return  Enum.GetValues(typeof(T));
        }

        public static string[] GetNames<T>(this T t)
        {
            return Enum.GetNames(typeof(T));
        }
    }

}
